package org.mnswf.services {
	
	import com.dannypatterson.remoting.FaultEvent;
	import com.dannypatterson.remoting.ResultEvent;
	import com.dannypatterson.remoting.ServiceProxy;
	import flash.events.EventDispatcher;
	import org.mnswf.model.Announcement;
	import com.dannypatterson.config.Settings;
	
	public class GetAnnouncementsService extends EventDispatcher implements IService {
	
		private var service:ServiceProxy;
		
		public function GetAnnouncementsService() {
			service = new ServiceProxy(Settings.getInstance().remotingGateway, Settings.getInstance().announcementService);
			service.addEventListener(ResultEvent.RESULT, onComplete, false, 0, true);
			service.addEventListener(FaultEvent.FAULT, onFault, false, 0, true);
		}
		
		private function onComplete(event:ResultEvent):void {
			var announcements:Array = new Array();
			for(var i:Number = 0; i < event.result.length; i++) {
				var announcement:Announcement = new Announcement();
				announcement.parseTransferObject(event.result[i]);
				announcements.push(announcement);
			}
			dispatchEvent(new ResultEvent(ResultEvent.RESULT, event.bubbles, event.cancelable, announcements));
		}
		
		private function onFault(event:FaultEvent):void {
			dispatchEvent(new FaultEvent(FaultEvent.FAULT, event.bubbles, event.cancelable, event.fault));
		}
		
		public function execute():void {
			service.getAnnouncements();
		}
		
	}
}